<template>
  <webview
    id="printWebview"
    ref="printWebview"
    :src="printUrl"
    nodeintegration
    style="visibility: hidden;width: 0;"
    @click="printClick"
  ></webview>
</template>
<script>
//引入ipcRenderer对象,该对象和主线程的ipcMain通讯
import { ipcRenderer } from "electron";
import path from "path";

export default {
  props: {
    printFlag: {
      type: Object
    }
  },
  data() {
    return {
      //  path.join(__static):指向的是该项目在的static文件夹在硬盘中绝对路径
      printUrl: path.join(__static, "./print.html")
    };
  },
  mounted() {

    var webview = this.$refs.printWebview;
    webview.addEventListener("ipc-message", () => {
      if (event.channel === "webview-print-do") {
        webview.print(
          {
            //是否是静默打印,true 为静默打印，false 会弹出打印设置框
            silent: true,
            printBackground: true,
            deviceName: this.printFlag.deviceName
          },
          data => {
            if (data == true) {
              this.$message({
                message: "打印信号发送成功",
                type: "success"
              });
              this.$emit("changeFlag");
            }
          }
        );
      }
    });

    ipcRenderer.on("print-error", (event, err) => {
       this.$message({
          message: "打印失败!",
          type: "error"
        });
        return false;
    });
  },
  watch: {
    printFlag: {
      deep: true,
      handler: function(newVal, oldVal) {
        if (newVal.getPrinters) {
          ipcRenderer.send("getPrinters");
          ipcRenderer.once("printName", (event, data) => {
            // data就是打印机列表
            this.filterPrinter(data);
          });
        }
        if (newVal.data) {
          var webview = this.$refs.printWebview;
          webview.click();
        }
      }
    }
  },
  methods: {
    // 获取打印机列表
    filterPrinter(data) {
      // var printList = data;
      var printList = data.filter(item => item.status === 0);
      console.log(printList, "===printList===");
      var mainForm = this.$S.get("settings-form");

      if (mainForm) {
        if (mainForm.printData.modeChildVal === "default") {
          var cb = "";
          printList.forEach(item => {
            if (item.isDefault) {
              cb = item.name;
            }
          });
          this.$store.state.deviceName = cb;
        } else {
          this.$store.state.deviceName = mainForm.printData.modeChildVal;
        }
      }
      // console.log(
      //   this.$store.state.deviceName,
      //   "===this.$store.state.deviceName默认打印机设备==="
      // );
      this.$emit("getPrinters", printList);
    },
    // 打印测试
    printClick() {
      var webview = this.$refs.printWebview;
      var data = {};
      data.deviceName = this.printFlag.deviceName;
      switch (this.printFlag.flag) {
        // printFlag.flag 0：打印小票测试 1：打印标签测试 2：销售后自动打印等
        case 0:
          data.title = "打印小票测试";
          data.data = "***打印小票测试********";

          break;
        case 1:
          data.title = "打印标签测试";
          data.data = "***打印标签测试********";
          break;
        case 2:
          data.title = "打印销售单";
          data.data = this.$S.get("allOrder")[0];
          data.encryption = this.printFlag.encryption;
          break;
        default:
          return;
          break;
      }
      // this.printFlag.padding：打印边距
      switch (this.printFlag.padding) {
        case "maxPadding":
          data.width = "80%";
          data.margin = "5%";
          break;
        case "minPadding":
          data.width = "88%";
          data.margin = "1%";
          break;
        default:
          data.width = "92%";
          data.margin = "0";
          break;
      }
      switch (this.printFlag.padding) {
        case "maxPadding":
          data.width = "80%";
          data.margin = "5%";
          break;
        case "minPadding":
          data.width = "86%";
          data.margin = "2%";
          break;
        default:
          data.width = "90%";
          data.margin = "0";
          break;
      }
      var obj = {
        data: data,
        flag: this.printFlag.flag || 0
      };
      // console.log(obj, "===obj==");

      webview.send("webview-print-render", obj);
    }
  }
};
</script>
<style  scope lang='scss'>
</style>